package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjWorkImage;
import com.zb.mapper.ZjWorkImageMapper;
import com.zb.service.ZjWorkImageService;
import com.zb.vo.OrderImageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 工单附件表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-20
 */
@Service
public class ZjWorkImageServiceImpl extends ServiceImpl<ZjWorkImageMapper, ZjWorkImage> implements ZjWorkImageService {

    // 新增、删除工单附件
    @Override
    public void save(Long custAccId, Long workId, List<OrderImageVo> imageVos, List<Long> delImageIds) {
        if (null != delImageIds && !delImageIds.isEmpty()) {
            this.removeByIds(delImageIds);
        }
        if (null == imageVos || imageVos.isEmpty()) {
            return;
        }
        List<ZjWorkImage> imageList = imageVos.stream().filter(img -> null == img.getId()).map(imgVo -> {
            ZjWorkImage image = new ZjWorkImage();
            BeanUtils.copyProperties(imgVo, image);
            image.setId(IdWorker.getId());
            image.setWorkId(workId);
            image.setCustAccId(custAccId);
            image.setImage(null == image.getImage()? "" : image.getImage());
            return image;
        }).toList();
        if (!imageList.isEmpty()) {
            this.saveBatch(imageList);
        }
    }

    // 根据工单ID获取工单图片
    @Override
    public List<ZjWorkImage> getByWorkId(Long workId) {
        QueryWrapper<ZjWorkImage> wrapper = new QueryWrapper<>();
        wrapper.select("id", "image");
        wrapper.eq("work_id", workId);
        return this.list(wrapper);
    }
}
